<?php
header('Content-Type: text/html; charset=utf-8');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
require_once dirname(__FILE__) . '/../../../incls/php/ecomutils.class.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = MiscUtils::getParam('action', '');
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'cancel':
      cancel($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function cancel($myPdo) {
  if (!isset($_SESSION['index' . USER])) {
    return;
  }
  $to = MiscUtils::getParam('to', '');
  $id = MiscUtils::getParam('id', 0);
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.order_number = :v1';
  $cond_vals->v = array(':v1' => $id);
  $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($tmp) > 0) {
    $tmp[0]->is_cancel = 1;
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $tmp[0]);
    EComUtils::update_stock($myPdo, $tmp[0], true);
  }
  echo "
  <script>
  parent.location.href = '$to';
  </script>
  ";
  return;
}
?>
